/**
 * UninaDocentiSync - A Java application to synchronize Unina resources
 * Copyright (C) 2011  Fabio Scippacercola (nonplay.programmer@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

package uninadocentisync.guibeans;

import java.awt.Component;
import java.util.Enumeration;
import java.util.HashMap;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeNode;
import uninadocenti.framework.WebDocentiFile;
import uninadocenti.framework.synch.SynchingFile;

/**
 *
 * @author user
 */
public class UninaTreeNode implements TreeNode {

    UninaTreeNode(HashMap<SynchingFile<WebDocentiFile>, UninaTreeNode> tree, SynchingFile<WebDocentiFile> resAssociated) {
        this.tree = tree;
        this.resAssociated = resAssociated;
    }

    public Enumeration children() {
        return new Enumeration() {
            int i = 0;
            public boolean hasMoreElements() {
                return (i < getChildCount());
            }

            public Object nextElement() {
                return (getChildAt(i++));
            }
        };
    }

    public boolean getAllowsChildren() {
        return resAssociated.hasChildren();
    }

    public TreeNode getChildAt(int i) {
        return tree.get(resAssociated.getChildren()[i]);
    }

    public int getChildCount() {
        return (resAssociated.getChildren() != null ? resAssociated.getChildren().length : 0);
    }

    public int getIndex(TreeNode tn) {
        for (int i = 0; i < getChildCount(); i++)
            if (tree.get(resAssociated.getChildren()[i]).equals(tn))
                return i;
        return -1;
    }

    public TreeNode getParent() {
        return tree.get(resAssociated.getFather());
    }

    public boolean isLeaf() {
        return !resAssociated.hasChildren();
    }

    @Override
    public String toString() {
        if (resAssociated == null || resAssociated.getResource() == null)
            return "Root";
        return resAssociated.getResource().getName();
    }
    
    
    
    protected HashMap<SynchingFile<WebDocentiFile>, UninaTreeNode> tree;
    protected SynchingFile<WebDocentiFile> resAssociated;

    static public class UninaTreeCellRender extends DefaultTreeCellRenderer
    {
        private static ImageIcon toSkipIcon = new ImageIcon(UninaTreeCellRender.class.getResource("/uninadocentisync/gui/res/toSkip.png"));
        private static ImageIcon skippedIcon = new ImageIcon(UninaTreeCellRender.class.getResource("/uninadocentisync/gui/res/skipped.png"));
        private static ImageIcon folderIcon = new ImageIcon(UninaTreeCellRender.class.getResource("/uninadocentisync/gui/res/folderIcon.png"));
        private static ImageIcon toDownloadIcon = new ImageIcon(UninaTreeCellRender.class.getResource("/uninadocentisync/gui/res/toDownload.png"));
        private static ImageIcon downloadedIcon = new ImageIcon(UninaTreeCellRender.class.getResource("/uninadocentisync/gui/res/downloaded.png"));

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            if (value instanceof UninaTreeNode)
            {
                final UninaTreeNode f = (UninaTreeNode)value;
                final SynchingFile.State s = f.resAssociated.getDownloadState();

                if (!f.isLeaf()) setIcon(folderIcon);
                else
                {
                    switch (s)
                    {
                        case TODOWNLOAD: { setIcon(toDownloadIcon); break; }
                        case DOWNLOADED: { setIcon(downloadedIcon); break; }
                        case TOSKIP: { setIcon(toSkipIcon); break; }
                        case SKIPPED: { setIcon(skippedIcon); break; }
                        case DOWNLOADING: { setIcon(toDownloadIcon); break; }
                    }
                }
            }
            return this;
        }


    }
}